package org.edu.common.utils;



import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类。
 * 
 */
public class DateUtil {
	
	
	public final static String TYPE1="yyyy-MM-dd HH:mm:ss";
	public final static String TYPE4="yyyy-MM-dd HH:mm";
	
	public final static String TYPE2="yyyy-MM-dd";
	
	public final static String TYPE3="yyyy年MM月dd日";
	public final static String TYPE6="yyyy年MM月dd日 HH:mm";
	public final static String TYPE5="yyyyMMddHHmmssSSS";
	/**
	 * 大写 星期几
	 */
	public final static String[] WEEKDAY_UPPER = {"日","一","二","三","四","五","六"};
	/**
	 * 格式当前日期
	 * 
	 * @return 格式化后的日期
	 */
	public static String getNow() {
		SimpleDateFormat sdf = new SimpleDateFormat(TYPE1);
		return (sdf.format(now()));
	}
	/**
	 * 格式当前日期
	 * 
	 * @return 格式化后的日期
	 */
	public static String getNow(String fs) {
		SimpleDateFormat sdf = new SimpleDateFormat(fs);
		return (sdf.format(now()));
	}
	/**
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDate(Date date, String pattern) {
		if (date == null) {
			return "";
		}
		if (pattern == null) {
			pattern = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return (sdf.format(date));
	}
	/**
	 * @param datetime
	 * @return
	 */
	public static Date parseDateTime(String datetime,String fs) {
		SimpleDateFormat formatter = new SimpleDateFormat(fs);
		if ((datetime == null) || (datetime.equals(""))) {
			return null;
		} else {
			try {
				return formatter.parse(datetime);
			} catch (ParseException e) {
				return parseDate(datetime);
			}
		}
	}
	/**
	 * 获取当前时间的年
	 * @return
	 */
	public static int getYear(){
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.YEAR);
	}
	
	/**
	 * 获取当前时间的月
	 * @return
	 */
	public static int getMonth(){
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.MONTH)+1;
	}
	
	/**
	 * 获取当前时间的日
	 * @return
	 */
	public static int getDay(){
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 获取当前时间的星期几
	 * @return
	 */
	public static int getWeekDay(){
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.DAY_OF_WEEK);
	}
	
	/**
	 * 将字符串格式化为日期
	 * 
	 * @param date
	 *            日期字符串
	 * @param fs
	 *            日期格式
	 * @return 日期
	 */
	public static Date parseDate(String date, String fs) {
		if (date==null||"".equals(date)) {
			return null;
		}
		// 日期格式
		SimpleDateFormat formatter = new SimpleDateFormat(fs);
		try {
			return formatter.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	

	
	/**
	 * @return
	 */
	public static Date now() {
		return (new Date());
	}
	

	
	/**
	 * @param date
	 * @return
	 */
	public static Date parseDate(String date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if ((date == null) || (date.equals(""))) {
			return null;
		} else {
			try {
				return formatter.parse(date);
			} catch (ParseException e) {
				return null;
			}
		}
	}
	
	/**
	 * @param date
	 * @return
	 */
	public static Date parseDate2(String date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		if ((date == null) || (date.equals(""))) {
			return null;
		} else {
			try {
				return formatter.parse(date);
			} catch (ParseException e) {
				return null;
			}
		}
	}
	
	/**
	 * @param datetime
	 * @return
	 */
	public static Date parseDate(Date datetime) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (datetime == null) {
			return null;
		} else {
			try {
				return formatter.parse(formatter.format(datetime));
			} catch (ParseException e) {
				return null;
			}
		}
	}
	
	/**
	 * @param o
	 * @return
	 */
	public static String formatDate(Object o) {
		if (o == null) {
			return "";
		}
		if (o.getClass() == String.class) {
			return formatDate((String) o);
		} else if (o.getClass() == Date.class) {
			return formatDate((Date) o);
		} else if (o.getClass() == Timestamp.class) {
			return formatDate(new Date(((Timestamp) o).getTime()));
		} else {
			return o.toString();
		}
	}
	
	/**
	 * @param o
	 * @return
	 */
	public static String formatDateTime(Object o) {
		if (o.getClass() == String.class) {
			return formatDateTime((String) o);
		} else if (o.getClass() == Date.class) {
			return formatDateTime((Date) o);
		} else if (o.getClass() == Timestamp.class) {
			return formatDateTime(new Date(((Timestamp) o).getTime()));
		} else {
			return o.toString();
		}
	}
	
	/**
	 * 给时间加上或减去指定毫秒，秒，分，时，天、月或年等，返回变动后的时间
	 * 
	 * @param date
	 *            要加减前的时间，如果不传，则为当前日期
	 * @param field
	 *            时间域，有Calendar.MILLISECOND,Calendar.SECOND,Calendar.MINUTE,<br> *
	 *            Calendar.HOUR,Calendar.DATE, Calendar.MONTH,Calendar.YEAR
	 * @param amount
	 *            按指定时间域加减的时间数量，正数为加，负数为减。
	 * @return 变动后的时间
	 */
	public static Date add(Date date, int field, int amount) {
		if (date == null) {
			date = new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, amount);
		return cal.getTime();
	}
	
	/**
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addMilliSecond(Date date, int amount) {
		return add(date, Calendar.MILLISECOND, amount);
	}
	
	/**
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addSecond(Date date, int amount) {
		return add(date, Calendar.SECOND, amount);
	}
	
	/**
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addMiunte(Date date, int amount) {
		return add(date, Calendar.MINUTE, amount);
	}
	
	/**
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addHour(Date date, int amount) {
		return add(date, Calendar.HOUR, amount);
	}
	
	/**
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addDay(Date date, int amount) {
		return add(date, Calendar.DATE, amount);
	}
	
	/**
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addMonth(Date date, int amount) {
		return add(date, Calendar.MONTH, amount);
	}
	
	/**
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addYear(Date date, int amount) {
		return add(date, Calendar.YEAR, amount);
	}
	


	
	/**
	 * 日期格式转换,从YYYYMMdd格式转换成YYYY-MM-dd
	 * 
	 * @param dateString
	 * @return
	 */
	public static String parseDateString(String dateString) {
		return formatDate(parseDate2(dateString));
	}
	/**
	 * 
	 * @return
	 */
    public static String formatTimeStamp(String dat, String tim,int len) { 
        SimpleDateFormat df = new SimpleDateFormat(dat); 
        if (tim.length()>len) {
        	return df.format(new Long(tim));
        }
        return tim;
    } 
    
    /**
     * 当月第一天
     * 
     * @author  linxw
     * @version  [版本号, 2014-10-9] 	
     */
    public static String getMonthFirstDay()
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        
        Calendar cal = Calendar.getInstance();
        //cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        
        return format.format(cal.getTime()); 
    }
    
    /**
     * 当月最后一天
     * 
     * @author  linxw
     * @version  [版本号, 2014-10-9]   
     */
    public static String getMonthLastDay()
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        
        Calendar cal = Calendar.getInstance();
        //cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        
        return format.format(cal.getTime()); 
    }
    
    /**
     * 时间比大小
     * 
     * @author  lhh
     * @return  a比b早,返回-1, a与b相同,返回0 a比b晚,返回1
     * @version  [版本号, 2015-3-16]   
     */
    public static int timeCompare(String a,String b){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        try {
            c1.setTime(formatter.parse(a));
            c2.setTime(formatter.parse(b));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int result=c1.compareTo(c2);
        return result;
    }
    
    /**
	 * 获取某个月的天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonthDays(String date) {

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
		calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(5, 7)) - 1);
		int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		return maxDay;
	}
	
	/**
	 * 获取某个月的所有天
	 * 
	 * @return
	 */
	public static Date[] getDates(String year, String month) {
		int maxDate = 0;
		Date first = null;
		try {
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			first = sdf.parse(year + month);
			cal.setTime(first);
			maxDate = cal.getMaximum(Calendar.DAY_OF_MONTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Date[] dates = new Date[maxDate];
		for (int i = 1; i <= maxDate; i++) {
			dates[i - 1] = new Date(first.getTime());
			first.setDate(first.getDate() + 1);
		}
		return dates;
	}
	
	/** * 获取指定日期是星期几

	  * 参数为null时表示获取当前日期是星期几

	  * @param date

	  * @return

	*/

	public static String getWeekOfDate(Date date) {      

	    String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};        
	    Calendar calendar = Calendar.getInstance();      
	    if(date != null){        
	         calendar.setTime(date);      
	    }        
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0){        
	        w = 0;      
	    }      
	    return weekOfDays[w];    
	}
	public static long getHourBetweenDays(String s_start , String e_end,String dateType){
		Date start = DateUtil.parseDateTime(s_start,dateType);
		Date end = DateUtil.parseDateTime(e_end,dateType);
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long diff = end.getTime() - start.getTime();
		long hour = diff % nd / nh;
		return hour;
	}
	
	/**
	 * 
	 * @param date1 <String>
	 * @param date2 <String>
	 * @return int
	 * @throws ParseException
	 */
	public static int getMonthSpace(String date1, String date2,String type)
			throws ParseException {

		int result = 0;
		int month=0;
		SimpleDateFormat sdf = new SimpleDateFormat(type);

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		c1.setTime(sdf.parse(date1));
		c2.setTime(sdf.parse(date2));
		int year = c2.get(Calendar.YEAR)- c1.get(Calendar.YEAR);
		month = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
		if(year!=0){
		
			result = year*12+month;
		}else{
			result = 	 month == 0 ? 1 : Math.abs(result);
		}
		return result;

	

	}
	/*public static void main(String[] args) throws ParseException {
		String s = "2015-04-18 20:30:39";
		String e= "2017-04-18 20:30:39";
	int r =	DateUtil.getMonthSpace(s,e,DateUtil.TYPE1);
		System.out.println(r);
	}*/
	


}
